#include <bits/stdc++.h>

using namespace std;

int n, ans = 0x3f3f3f3f, y;
const int N = 1e5 + 10;
int maxn;
vector<int> a[N], k;
int b[N];
bool vis[N];
bool dfs1(int x, int f) {
	 for(auto &j: a[x]){
	 	if()
	 }
}
int dfs(int x, int f) {
	int y = 0;
	int maxn = -1;
	for (auto &j: a[x]) {
		if(j == f) continue;
		if(b[j] > b[x]) y = 1, maxn =max(dfs(j, x) + 1, maxn);
	}
	if(!y)
	return maxn;
}
int main() {
    int t;
    scanf("%d", &t);
    while (t--) {
		scanf("%d", &n);
		int m = n - 1;
		while (m--) {
			int b1, c;
			scanf("%d%d", &b1, &c);
			a[b1].push_back(c);
			a[c].push_back(b1);
		}
		for (int i = 1; i <= n; i++){
			scanf("%d", &b[i]);
		}
		for (int i = 1; i <= n; i++) {
			k.clear();
			memset(vis, 0, sizeof vis);
			dfs(i, 0);
			cout << k.size() << endl;
		}
	}
	return 0;
}
